package vip.xiaonuo.modular.attendancerecord.listener;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import reactor.core.Reactor;
import reactor.event.Event;
import reactor.function.Consumer;
import vip.xiaonuo.facedoor.modular.memberuser.entity.MemberUser;
import vip.xiaonuo.facedoor.modular.topic.EventTopic;
import vip.xiaonuo.modular.attendancerecord.entity.AttendanceRecord;
import vip.xiaonuo.modular.attendancerecord.service.AttendanceRecordService;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import static reactor.event.selector.Selectors.$;

@Service
@Slf4j
public class MemberUserDeleteListener_Attendance implements Consumer<Event<MemberUser>>, InitializingBean {

    @Resource
    private AttendanceRecordService service;

    @Autowired
    private Reactor reactor;

    private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd 23:59:59");

    private static SimpleDateFormat sdf_ = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    @Override
    public void accept(Event<MemberUser> event) {
        MemberUser data = event.getData();
        Long member_id = data.getId();
        Date today = new Date();
        try {
            today = sdf_.parse(sdf.format(today));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        service.remove(new LambdaQueryWrapper<AttendanceRecord>()
                .eq(AttendanceRecord :: getMemberUserId, member_id)
                .gt(AttendanceRecord :: getStartDate, today));
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        reactor.on($(EventTopic.MEMBER_DELETE), this);
    }



}
